#!/bin/bash

function exportVar {
  NAME=$1
  VALUE=$2
  echo export $NAME=\"$VALUE\"
  export $NAME="$VALUE"
}

function setenv {
  if [ -z "$1" ]; then
    echo "ERROR: setenv() requires one argument."
    exit 1
  fi
  if [ -z "${NACL_SDK_ROOT}" ]; then
    echo "ERROR: This script requires NACL_SDK_ROOT to be set."
    exit 1
  fi

  ARCH_WIDTH=$1
  if [ ${ARCH_WIDTH} = "32" ]; then
    CROSS_ID=i686
  elif [ ${ARCH_WIDTH} = "64" ]; then
    CROSS_ID=x86_64
  else
    echo "ERROR: Unknown arch width: ${ARCH_WIDTH}"
    exit 1
  fi

  OS_NAME=$(uname -s)
  if [ $OS_NAME = "Darwin" ]; then
    OS_SUBDIR="mac"
    OS_SUBDIR_SHORT="mac"
    OS_JOBS=4
  elif [ $OS_NAME = "Linux" ]; then
    OS_SUBDIR="linux"
    OS_SUBDIR_SHORT="linux"
    OS_JOBS=4
  else
    OS_SUBDIR="windows"
    OS_SUBDIR_SHORT="win"
    OS_JOBS=1
  fi

  NACL_TOOLCHAIN_ROOT=${NACL_SDK_ROOT}/toolchain/${OS_SUBDIR_SHORT}_x86_newlib
  NACL_BIN_PATH=${NACL_TOOLCHAIN_ROOT}/bin
  export NACL_CROSS_PREFIX=${CROSS_ID}-nacl

  if [[ -z "$NACL_MAKE_CCACHE" ]]; then
    exportVar NACLCC ${NACL_BIN_PATH}/${NACL_CROSS_PREFIX}-gcc
    exportVar NACLCXX ${NACL_BIN_PATH}/${NACL_CROSS_PREFIX}-g++
  else
    exportVar NACLCC "${NACL_MAKE_CCACHE} ${NACL_BIN_PATH}/${NACL_CROSS_PREFIX}-gcc"
    exportVar NACLCXX "${NACL_MAKE_CCACHE} ${NACL_BIN_PATH}/${NACL_CROSS_PREFIX}-g++"
  fi
  exportVar NACLAR ${NACL_BIN_PATH}/${NACL_CROSS_PREFIX}-ar
  exportVar NACLRANLIB ${NACL_BIN_PATH}/${NACL_CROSS_PREFIX}-ranlib
  exportVar NACLLD ${NACL_BIN_PATH}/${NACL_CROSS_PREFIX}-ld
  exportVar NACLSTRINGS ${NACL_BIN_PATH}/${NACL_CROSS_PREFIX}-strings
  exportVar NACLSTRIP ${NACL_BIN_PATH}/${NACL_CROSS_PREFIX}-strip

  exportVar CC "${NACLCC}"
  exportVar CXX "${NACLCXX}"
  exportVar AR "${NACLAR}"
  exportVar RANLIB "${NACLRANLIB}"
  exportVar PATH ${NACL_BIN_PATH}:${PATH};
  exportVar C_INCLUDE_PATH "${NACL_SDK_ROOT}/include:${NACL_SDK_ROOT}/ports/include:${C_INCLUDE_PATH}"
  exportVar CPLUS_INCLUDE_PATH "${NACL_SDK_ROOT}/include:${NACL_SDK_ROOT}/ports/include:${CPLUS_INCLUDE_PATH}"
  exportVar GYP_DEFINES "skia_os=nacl skia_gpu=0 skia_arch_width=${ARCH_WIDTH}"
}

function build {
  if [ -z "$1" ]; then
    echo "ERROR: build() requires one argument."
    exit 1
  fi
  setenv $1

  export SKIA_OUT=out/nacl$1
  make ${MAKE_ARGS}
}

MAKE_ARGS=""

while (( "$#" )); do
  if [[ "$1" == "--use-ccache" ]];
  then
    if [[ -z "$NACL_MAKE_CCACHE" ]];
    then
      NACL_MAKE_CCACHE=$(which ccache)
    fi
  elif [ -z "${MAKE_ARGS}" ]; then
    MAKE_ARGS="$1"
  else
    MAKE_ARGS="${MAKE_ARGS} $1"
  fi
  shift
done

if [[ -n "$NACL_MAKE_CCACHE" ]]; then
  $NACL_MAKE_CCACHE --version &> /dev/null
  if [[ "$?" != "0" ]]; then
    echo "Unable to find ccache!"
    exit 1
  fi
fi

build 32 && \
build 64 && \
if ! [ -L platform_tools/nacl/out ]; then
  ln -s ../../out platform_tools/nacl
fi